package com.multi.tool.stream;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhengpeng.wang
 */
public class Test {

    public static void main(String[] args) {
        List<Integer> nums = Lists.newArrayList(123, 456);
        List<Integer> reorderNums = reorder(nums);
        System.out.println(JSON.toJSONString(nums));


        List<Demo> demos = Lists.newArrayList();
        Demo demo = new Demo();
        demo.setNum(5678);
        demo.setCode("5678str");
        demos.add(demo);
        demo = new Demo();
        demo.setNum(1234);
        demo.setCode("1234str");
        demos.add(demo);
        demo = new Demo();
        demo.setNum(1234);
        demo.setCode("1234str");
        demos.add(demo);
        List<Demo> collect = demos.stream().filter(demo1 -> !demo1.getNum().equals(1234)).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(demos) + "======" + JSON.toJSONString(collect));

        List<Demo> collect1 = demos.stream().sorted(Comparator.comparing(Demo::getNum)).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(demos) + "======" + JSON.toJSONString(collect1));

        System.out.println(true && false || true);
        System.out.println(true && false || true && false);

//        Map<String, Demo> stringDemoMap = demos.stream().collect(Collectors.toMap(Demo::getCode, Function.identity()));
//        System.out.println(stringDemoMap);
        Map<String, Integer> stringIntegerMap = demos.stream().collect(Collectors.toMap(Demo::getCode, Demo::getNum, (o1, o2) -> o1));
        System.out.println(stringIntegerMap);
    }

    private static List<Integer> reorder(List<Integer> nums) {
        nums.add(789);
        return null;
    }

    static class Demo implements Comparable {
        Integer num;
        String code;

        public Integer getNum() {
            return num;
        }

        public void setNum(Integer num) {
            this.num = num;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        @Override
        public int compareTo(Object o) {
            return 0;
        }
    }
}
